package com.isunland.managesystem.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.PicsGridViewAdapter;
import com.isunland.managesystem.base.BaseArray;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.MyGridView;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.ConmpanyEventsPics;
import com.isunland.managesystem.entity.ConmpanyEventsPicsOriginal;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import org.json.JSONException;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/5/26 11:10
 */
public class ConmpanyEventsPicsFragment extends BaseFragment {
  protected static final String EXTRA_ID =
      ConmpanyEventsPicsActivity.class.getSimpleName() + "EXTRA_ID";
  @BindView(R.id.gv_pics) MyGridView gvPics;
  private String mId;
  private ArrayList<ConmpanyEventsPics> mList;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom("图片查看");
    mId = getArguments().getString(EXTRA_ID);
    mList = new ArrayList<>();
    //获取图片
    getPics();
  }

  private void getPics() {
    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_COMPANY_EVENTS_PICS);
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("id", mId);
    MyUtils.showDialogLoading(getActivity());
    mActivity.volleyPost(url, params.getParams(), new VolleyResponse() {
      @Override public void onVolleyError(VolleyError arg0) {

      }

      @Override public void onVolleyResponse(String json) throws JSONException {
        MyUtils.dismissDialog();
        ConmpanyEventsPicsOriginal original =
            new Gson().fromJson(json, ConmpanyEventsPicsOriginal.class);
        ArrayList<ConmpanyEventsPics> rows = original.getRows();
        if (original != null && BaseArray.SUCCESS == original.getResult() && rows != null) {
          mList.clear();
          mList.addAll(rows);
          PicsGridViewAdapter mAdapter = new PicsGridViewAdapter(getActivity(), mList);
          gvPics.setAdapter(mAdapter);
        } else {
          ToastUtil.show("获取图片失败");
        }
      }
    });
  }

  @Nullable @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_events_pics, container, false);
    ButterKnife.bind(this, view);
    gvPics.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        //查看大图
        Intent intent = new Intent(getActivity(), CompanyForumPictureActivity.class);
        intent.putExtra(CompanyForumPictureFragment.EXTRA_PICTUREA, mList.get(i).getFilepath());
        intent.putExtra(CompanyForumPictureFragment.TYPE, CompanyForumPictureFragment.TYPEA);
        startActivity(intent);
      }
    });
    return view;
  }

  static ConmpanyEventsPicsFragment newInstance(String id) {
    Bundle args = new Bundle();
    args.putString(EXTRA_ID, id);
    ConmpanyEventsPicsFragment fragment = new ConmpanyEventsPicsFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
